/*
  Copyright 2011 Jonathan Albarrán León

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package com.jaal.movilconsultanet.asynctasks;

import java.util.Map;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.jaal.movilconsultanet.Progreso;
import com.jaal.movilconsultanet.R;
import com.jaal.movilconsultanet.XMLContent;
import com.jaal.movilconsultanet.activities.Consulta;

public class GSMSessionFinalizer extends AsyncTask<String, Progreso, Boolean> {

	Consulta mainActivity;
	ProgressDialog progreso;
	Progreso progresoInfo = new Progreso();

	public GSMSessionFinalizer(Consulta mainActivity) {
		this.mainActivity = mainActivity;
	}
	
	@Override
	protected void onPreExecute() {
		progreso = new ProgressDialog(mainActivity);
		progreso.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		progreso.setProgress(0);
		progreso.setMessage(mainActivity.getResources().getText(R.string.session_closing));
		progreso.setCancelable(false);
		progreso.show();
	}
	
	@Override
	protected Boolean doInBackground(String... urls) {

		Log.d("MCN", "GSMSessionFinalizer - inicio");

		progresoInfo.setProgreso(40);
		progresoInfo.setMensaje(mainActivity,R.string.session_verifying);
		publishProgress(progresoInfo);

		try {

			Map<String, String> mapa = XMLContent.getXML("sesiones");

			mapa.put("session_started", "false");
			mapa.put("session_id", "null");

			progresoInfo.setProgreso(70);
			progresoInfo.setMensaje(mainActivity,R.string.session_deleting);
			publishProgress(progresoInfo);

			XMLContent.saveXML("sesiones", mapa);

			progresoInfo.setProgreso(100);
			progresoInfo.setMensaje(mainActivity,R.string.session_deleted);
			progresoInfo.setFinished(true);
			publishProgress(progresoInfo);
			
		} catch (Exception e) {
			progresoInfo.setError(true);
			progresoInfo.setMensaje(mainActivity,R.string.error);
			publishProgress(progresoInfo);
			e.printStackTrace();
		}
		return false;
	}

	@Override
	protected void onProgressUpdate(Progreso... values) {

		Progreso mProgresoInfo = values[0];

		if (!mProgresoInfo.isError()) {
			progreso.setProgress(mProgresoInfo.getProgreso());
			progreso.setMessage(mProgresoInfo.getMensaje());

			if (mProgresoInfo.isFinished()) {
				progreso.dismiss();
			}

		} else {
			progreso.dismiss();
			Toast.makeText(mainActivity, mProgresoInfo.getMensaje(),
					Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	protected void onPostExecute(Boolean result) {

		if (progresoInfo.isError()) {
			return;
		}
		mainActivity.finish();
	}

}
